package thread4.TEST;
import javax.swing.plaf.SliderUI;

public class test2 {//一个public最多外展象征的也最多一个类
    public void methoud() {

    }
}

class test3 extends test2 {
    public void main1() throws InterruptedException {
        methoud();
        int a = 10;
        this.methoud();
        Object object = new Object();
//        object.wait();
        Thread thread1 = new Thread(() -> {
            synchronized (Thread.currentThread()) {
                try {
                    Thread.currentThread().wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            Thread thread2 = new Thread(() -> {
               this.methoud();
                Thread cur = Thread.currentThread();//拿到thread2
               //如何拿到thread1?
            });
//           Thread thread3 = new Thread(new Runnable() {
//               @Override
//               public void run() {
////                   a = 20;
//               }
//           });
        });
        thread1.start();
        Thread.sleep(3000);
        synchronized (thread1) {
            thread1.notify();
        }
        Thread.sleep(3000);
        thread1.join();//你里面的wait?，不需要拿到锁？不需要拿到锁，它是以thread1自己(this)为厌弃锁
        //join是抢有锁吗？
        //从类与对象体系里面调用的wait、notify不需要争锁环境
        synchronized (thread1) {

        }


    }

    public static void main(String[] args) throws InterruptedException {
        test3 test = new test3();
        test.main1();
    }
}
class test4 {
    public static void main(String[] args) throws InterruptedException {
        Thread thread1 = new Thread(() -> {
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            //thread1销毁时会不会唤醒所有厌thread1锁的代码块，会！
        });
        thread1.start();
        synchronized (thread1) {
            thread1.wait();
        }

    }
}
class test5 {
    public static void main(String[] args) throws InterruptedException {
        Thread thread1 = new Thread(() -> {
            synchronized (Thread.currentThread()) {
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        thread1.start();
        Thread.sleep(1000);
        /*synchronized (thread1) {
            System.out.println("hello");
        }*/
        thread1.join();
    }
}
class test6 {
    public static void main(String[] args) {
        Object obejct1 = new Object();
        Object obejct2 = new Object();
        /*synchronized (obejct1,obejct2) {

        }*/
        Thread thread1 = new Thread(() -> {
            synchronized (obejct1) {

            }
        });
        Thread thread2 = new Thread(() -> {
            synchronized (obejct1) {
                /*try {
                    obejct1.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }*/
            }
            /*try {
                thread1.wait();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }*/
        });
        thread1.start();
        thread2.start();
/*
        synchronized (lock) {

        }
        synchronized (lock) {

        }
        synchronized (lock) {

        }
        synchronized (lock) {

        }
*/
    }
}
class SingletonHungry {
    private static SingletonHungry instance = new SingletonHungry();//static静态存储,只有类加载时的创建一次一个
    private SingletonHungry() {}//private封闭构造器
    public static SingletonHungry getInstance() {//封装好获取
        return instance;
    }
}
class SingletonLazy {
    private static SingletonLazy instance = null;//static静态存储
    private SingletonLazy() {}//private封闭构造器
    public static SingletonLazy getInstance() {
        if (instance == null) { //优化时 线程不加锁能过(只读)情况 就拎开 少加点锁 分开处理掉
            synchronized (SingletonLazy.class) {//写的这块范围内 线程安全问题保障:
                if (instance == null) {//->连续读取instance的null时，两次没有前后影响到 都读为null 都去创建实例
                    instance = new SingletonLazy();
                }
                return instance;
            }
        }
        return instance;
    }
}
